package com.sqnu.dashe.utils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

/**
 * @author mmy
 * @create 2024-03-23 19:58
 * @description 邮箱发送验证码
 */
public class MailUtils {

    public static void sendTestMail(String email, String code) throws MessagingException {
        // 创建Properties 类用于记录邮箱的一些属性
        Properties props = new Properties();
        // 表示SMTP发送邮件，必须进行身份验证
        props.put("mail.smtp.auth", "true");
        //此处填写SMTP服务器
        props.put("mail.smtp.host", "smtp.qq.com");
        //端口号，QQ邮箱端口587
        props.put("mail.smtp.port", "587");
        // 此处填写，写信人的账号
        props.put("mail.user", "2981789369@qq.com");
        // 此处填写16位STMP口令
        props.put("mail.password", "twgqhtxuyerpddbj");
        // 构建授权信息，用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                // 用户名、密码
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        // 使用环境属性和授权信息，创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
        // 创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);
        // 设置发件人
        InternetAddress form = new InternetAddress(props.getProperty("mail.user"));
        message.setFrom(form);
        // 设置收件人的邮箱
        InternetAddress to = new InternetAddress(email);
        message.setRecipient(RecipientType.TO, to);
        // 设置邮件标题
        message.setSubject("达舍校园");
        // 设置邮件的内容体
        message.setContent("<html><body><br>"
                + "<div style='text-align: center;'><img src='https://myspringboottest.oss-cn-hangzhou.aliyuncs.com/dashexioayuan.png?Expires=1711238419&OSSAccessKeyId=TMP.3KiRHZ8dog3Fnn7sxG5a5qNGhQDoMG2KnwQkdt7nrcDZRJQLCb1Ywcdv3oDntGTsqRxpJ1oqEP1p5JUNn7qNXgr1hjGzUL&Signature=IeUl%2BK7uCzzcrD0QOxlHrPFu34g%3D' alt='达舍校园' style='max-width: 100%; height: auto;'></div><br>"
                + "<div style='font-size: 18px; font-family: 微软雅黑; text-align: center;'>"
                + "验证码:<br>"
                + "<div style='margin: auto; border: 0px solid #000; background-color: #FFF0F5; width: fit-content; padding: 10px;'>"
                + code
                + "</div>"
                + "用于注册达舍校园账号<br>"
                + "五分钟内有效,请勿泄露和转发，如非本人操作，请忽略此邮件。</div>"
                + "</body></html>", "text/html;charset=UTF-8");


        // 最后当然就是发送邮件啦
        Transport.send(message);
    }

    public static String achieveCode() {
        String[] beforeShuffle = new String[]{"0","1","2", "3", "4", "5", "6", "7", "8", "9"};
        List<String> list = Arrays.asList(beforeShuffle);//将数组转换为集合
        Collections.shuffle(list);  //打乱集合顺序
        StringBuilder sb = new StringBuilder();
        for (String s : list) {
            sb.append(s); //将集合转化为字符串
        }
        return sb.substring(2, 8);
    }
}
